package lib.operation;

import java.sql.*;
import java.util.Scanner;

/**
 * @author FBI
 * 2022-02-06 19:45
 */


//改，update，针对借阅状态进行修改, 1 是未借阅 ， 0 是已借阅
public class Borrow implements Do{

    @Override
    public void work(){
        System.out.println("=====借阅书籍=====");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入书名");
        String name = sc.nextLine();  //name为所要借阅书名

        //借
        try {
            Borrow.borrowBook(name);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    //借书
    public static void borrowBook(String name) throws Exception {
        Connection conn = null;
        Statement stmt = null;
        ResultSet resultSet = null;
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/lib", "root", "2506642408");
        stmt = conn.createStatement();
        resultSet = stmt.executeQuery("select * from book where name = "+"'"+ name + "'");
        String s = "";
        while (resultSet.next()){
            s = resultSet.getString("isBorrowed");
        }
        if ("1".equals(s)){
            String sql = "update book set isBorrowed = 0 where name = "+"'"+ name + "'";
            int i = stmt.executeUpdate(sql);
            System.out.println(i== 1 ? "借阅成功":"借阅失败");
        }else if ("0".equals(s)){
            System.out.println("已借阅");
        }else {
            System.out.println("请输入正确的书名");
        }


    }



}